//
//  UserCenterGroup.m
//  cutter
//
//  Created by mac on 16/1/5.
//  Copyright © 2016年 e360. All rights reserved.
//

#import "UserCenterGroup.h"
@implementation UserCenterGroupCell
@end

//
@implementation UserCenterGroup
-(void)viewDidLoad{
    [super viewDidLoad];
    [self setData:[AppData userClientGroup:[TMGlobal valueForKey:@"user"]]];
}
//
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.data count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserCenterGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    [cell.nameView setText:[NSString stringWithFormat:@"%@",[[self.data objectAtIndex:indexPath.row] objectForKey:@"groupname"]]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    id item = [self.data objectAtIndex:indexPath.row];
    if (self.isEdit == NO && self.onSelected) {
        self.onSelected(self, item);
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if ([self.data count] > 0) {
        return @"已有分组";
    }
    return nil;
}
//
-(IBAction)doneTouch:(id)sender {
    if ([self.nameView.text length] == 0) {
        [UIAlertController show:nil message:@"分组名不能为空!" buttons:nil handler:nil];
        return;
    }
    //
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.nameView.text forKey:@"groupname"];
    [dic setValue:[NSString uuid] forKey:@"uuid"];
    //用户
    id user = [TMGlobal valueForKey:@"user"];
    if (user) {
        [dic setValue:[user objectForKey:@"id"] forKey:@"userid"];
    }
    //
    __weak UserCenterGroup *this = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id group = [AppData userClientEditGroup:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:this.view animated:YES];
            if (self.onSelected) {
                this.onSelected(this, group);
            }
        });
    });
}
-(IBAction)closeTouch:(id)sender {
    if (self.onSelected) {
        self.onSelected(self, nil);
    }
}
@end


@implementation UserCenterEditGroup
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.tableView setEditing:YES];
    [self.nameView setDelegate:self];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        __weak UserCenterEditGroup *this = self;
        [UIAlertController show:nil message:@"删除当前分组!" buttons:@[@"删除",@"取消"] handler:^(UIAlertAction *action){
            if ([action.title isEqualToString:@"删除"]) {
                id item = [this.data objectAtIndex:indexPath.row];
                if ([AppData userClientDeleteGroup:item]) {
                    [this.data removeObjectAtIndex:indexPath.row];
                    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                }
            }
        }];
    }
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    if ([sourceIndexPath isEqual:destinationIndexPath] == NO) {
        [self.data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
        //
        for (int i=0; i<self.data.count; i++) {
            id group = [self.data objectAtIndex:i];
            [group setValue:[NSNumber numberWithInt:i] forKey:@"sort"];
        }
        [self setChanged:YES];
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.nameView.text length] == 0) {
        [UIAlertController show:nil message:@"分组名不能为空!" buttons:nil handler:nil];
        return NO;
    }
    //
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.nameView.text forKey:@"groupname"];
    [dic setValue:[NSString uuid] forKey:@"uuid"];
    //用户
    id user = [TMGlobal valueForKey:@"user"];
    if (user) {
        [dic setValue:[user objectForKey:@"id"] forKey:@"userid"];
    }
    //
    __weak UserCenterGroup *this = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id group = [AppData userClientEditGroup:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:this.view animated:YES];
            if (self.onSelected) {
                this.onSelected(this, group);
            }
        });
    });
    return YES;
}
-(IBAction)doneTouch:(id)sender {
    if (self.changed) {
        for (id group in self.data) {
            [AppData userClientEditGroup:group];
        }
    }
    //
    if (self.onSelected) {
        self.onSelected(self, nil);
    }
}
@end